<template>
    <div>
      <!-- 搜索结果页-头部导航 -->
      <div class="search-result-container">
        <!-- 点击实现后退效果 -->
        <van-nav-bar
          title="搜索结果"
          left-arrow
          @click-left="$router.go(-1)"
          fixed
        />
      </div>
      <div class="search-result-list">
       <van-list
       v-model="loading"
       :finished="done"
       finished-text="劳资是有底线的"
       offset="80"
       :immediate-check='false'
       @load="pullUpLoadMore"
       >
          <ArticleItem
          v-for="obj in resultList"
          :key="obj.art_id"
          :artObj="obj"
          :showCross="false"/>
       </van-list>
      </div>
    </div>
</template>

<script>
import { getSearchResultListAPI } from '@/api/search'
export default {
  name: 'SearchResultList',
  data () {
    return {
      resultList: [], // 搜索结果列表
      loading: false, // 每一页数据的加载状态
      done: false, // 全部加载完成状态,不触发load
      page: 1
    }
  },
  async created () {
    // 获取搜索结果列表
    const res = await getSearchResultListAPI({
      keyword: this.$route.params.name
    })
    // console.log(this.resultList)
    this.resultList = res.data.results
  },
  methods: {
    // 上拉加载更多
    async pullUpLoadMore () {
      const res = await getSearchResultListAPI({
        keyword: this.$route.params.name,
        page: ++this.page
      })
      const { results } = res.data
      if (results.length < 10) {
        this.done = true
        return
      }
      this.resultList = [...this.resultList, ...results]
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
